package com.wangzeqi.neihanduanzi.home.recommend;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.wangzeqi.neihanduanzi.R;
import com.wangzeqi.neihanduanzi.web.WebViewActivity;
import com.wangzeqi.neihanduanzi.cache.AllLruCacheLoad;

import com.wangzeqi.neihanduanzi.entity.AllEntity;
import com.wangzeqi.neihanduanzi.entity.GroupEntity;

/**
 * Created by dllo on 16/1/19.
 */
public class AdapterRecommend extends RecyclerView.Adapter {
    private Context context;
    private AllEntity allEntity;
    private RequestQueue queue;

    public AdapterRecommend(Context context, AllEntity allEntity) {
        this.context = context;
        this.allEntity = allEntity;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType==0){
            View view = LayoutInflater.from(context).inflate(R.layout.null_item, null);
            PictureViewHolderNull viewHolderNull = new PictureViewHolderNull(view);
            return viewHolderNull;
        }else if (viewType==1){
            View view = LayoutInflater.from(context).inflate(R.layout.video_item, null);
            VideoViewHolder viewHolder = new VideoViewHolder(view);
            return viewHolder;
        }else if (viewType==2){
            View view = LayoutInflater.from(context).inflate(R.layout.picture_item, null);
            PictureViewHolder viewHolder = new PictureViewHolder(view);
            return viewHolder;
        }
        else {
            View view = LayoutInflater.from(context).inflate(R.layout.jokes_item, null);
            JokesViewHolder viewHolder = new JokesViewHolder(view);
            return viewHolder;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        queue = Volley.newRequestQueue(context);
        AllLruCacheLoad cache = new AllLruCacheLoad();
        ImageLoader imageloader = new ImageLoader(queue,cache);
        final GroupEntity entity = allEntity.getData().getData().get(position).getGroup();
        if (getItemViewType(position)==0){
            PictureViewHolderNull viewHolderNull = (PictureViewHolderNull) holder;
            viewHolderNull.textView.setText(null);
        }else if (getItemViewType(position)==1){
            final VideoViewHolder videoViewHolder = (VideoViewHolder) holder;
            videoViewHolder.name.setText(entity.getUser().getName());
            videoViewHolder.content.setText(entity.getContent());
            videoViewHolder.type.setText(entity.getCategory_name());
            videoViewHolder.like.setText(entity.getDigg_count());
            videoViewHolder.dislike.setText(entity.getBury_count());
            videoViewHolder.comment.setText(entity.getComment_count());
            videoViewHolder.forward.setText(entity.getShare_count());
            videoViewHolder.imageView.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
            videoViewHolder.imageView.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
            videoViewHolder.imageView.setImageUrl(entity.getUser().getAvatar_url(), imageloader);


            videoViewHolder.videoImageView.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
            videoViewHolder.videoImageView.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
            videoViewHolder.videoImageView.setImageUrl("http://p2.pstatp.com/large/" + entity.getCover_image_uri(), imageloader);
            videoViewHolder.videoImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    videoViewHolder.imageViewPlay.setVisibility(View.INVISIBLE);
                    videoViewHolder.progressBar.setVisibility(View.VISIBLE);
                    videoViewHolder.videoView.setMediaController(new MediaController(context));
                    videoViewHolder.videoView.setVideoURI(Uri.parse(entity.getMp4_url()));
                    videoViewHolder.videoView.requestFocus();
                    videoViewHolder.videoView.start();
                    videoViewHolder.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            videoViewHolder.progressBar.setVisibility(View.INVISIBLE);
                            videoViewHolder.videoImageView.setVisibility(View.INVISIBLE);
                            videoViewHolder.videoView.setVisibility(View.VISIBLE);

                        }
                    });
                    videoViewHolder.videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {

                        @Override
                        public boolean onError(MediaPlayer mp, int what, int extra) {
                            Toast.makeText(context, "网路不给力啊！！！", Toast.LENGTH_SHORT).show();
                            videoViewHolder.progressBar.setVisibility(View.INVISIBLE);
                            videoViewHolder.imageViewPlay.setVisibility(View.VISIBLE);
                            return false;
                        }
                    });

                    videoViewHolder.videoView.start();
                }
            });
            videoViewHolder.personHome.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "个人主页", Toast.LENGTH_SHORT).show();
                }
            });
            videoViewHolder.detailHome.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(context,WebViewActivity.class);
                    intent.putExtra("url",entity.getShare_url());
                    context.startActivity(intent);
                }
            });
        }else if (getItemViewType(position)==2){
            PictureViewHolder viewHolder = (PictureViewHolder) holder;
            viewHolder.name.setText(entity.getUser().getName());
            viewHolder.content.setText(entity.getContent());
            viewHolder.type.setText(entity.getCategory_name());
            viewHolder.like.setText(entity.getDigg_count());
            viewHolder.dislike.setText(entity.getBury_count());
            viewHolder.comment.setText(entity.getComment_count());
            viewHolder.forward.setText(entity.getShare_count());
            viewHolder.imageView.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
            viewHolder.imageView.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
            viewHolder.imageView.setImageUrl(entity.getUser().getAvatar_url(), imageloader);
            Glide.with(context).load(entity.getLarge_image().getUri()).into(viewHolder.imageViewP);
//            viewHolder.imageViewContent.setDefaultImageResId(R.mipmap.ic_launcher);
//            viewHolder.imageViewContent.setErrorImageResId(R.mipmap.ic_launcher);
//            viewHolder.imageViewContent.setImageUrl(entity.getLarge_image().getUrl_list().get(0).getUrl(),imageloader);
            viewHolder.personHome.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "个人主页", Toast.LENGTH_SHORT).show();

                }
            });
            viewHolder.detailHome.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context,WebViewActivity.class);
                    intent.putExtra("url","http://neihanshequ.com/p"+entity.getShare_url().substring(37)
                    );
                    context.startActivity(intent);
                }
            });
        }else if (getItemViewType(position)==3){
            JokesViewHolder viewHolder = (JokesViewHolder) holder;
            viewHolder.name.setText(entity.getUser().getName());
            viewHolder.content.setText(entity.getContent());
            viewHolder.type.setText("" + entity.getCategory_name());
            viewHolder.like.setText("" + entity.getDigg_count());
            viewHolder.dislike.setText("" + entity.getBury_count());
            viewHolder.comment.setText("" + entity.getComment_count());
            viewHolder.forward.setText("" + entity.getShare_count());

            viewHolder.imageView.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
            viewHolder.imageView.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
            viewHolder.imageView.setImageUrl(entity.getUser().getAvatar_url(), imageloader);
            viewHolder.imageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            viewHolder.personHome.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "个人主页", Toast.LENGTH_SHORT).show();
                }
            });
            viewHolder.detailHome.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context,WebViewActivity.class);
                    intent.putExtra("url","http://neihanshequ.com/p"+entity.getShare_url().substring(37));
                    context.startActivity(intent);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return allEntity.getData().getData().size();
    }


    @Override
    public int getItemViewType(int position) {
        GroupEntity entity = allEntity.getData().getData().get(position).getGroup();
        if (entity == null) {
            return 0;
        } else if (entity != null && entity.getMp4_url() != null) {
            return 1;
        } else if (entity != null && entity.getLarge_image() != null) {
            return 2;
        } else {
            return 3;
        }
    }

    class JokesViewHolder extends RecyclerView.ViewHolder {

        TextView name, like, dislike, comment, forward, content, type;
        NetworkImageView imageView;
        ImageButton imageButton;
        LinearLayout personHome;
        RelativeLayout detailHome;

        public JokesViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.jokes_person_name);
            like = (TextView) itemView.findViewById(R.id.jokes_like_num);
            dislike = (TextView) itemView.findViewById(R.id.jokes_dislike_num);
            comment = (TextView) itemView.findViewById(R.id.jokes_comments_num);
            forward = (TextView) itemView.findViewById(R.id.jokes_forwarding_num);
            content = (TextView) itemView.findViewById(R.id.jokes_jokes_content);
            type = (TextView) itemView.findViewById(R.id.jokes_content_type);
            imageView = (NetworkImageView) itemView.findViewById(R.id.jokes_person_picture);
            imageButton = (ImageButton) itemView.findViewById(R.id.jokes_dislike_del);
            personHome = (LinearLayout) itemView.findViewById(R.id.jokes_person_home);
            detailHome = (RelativeLayout) itemView.findViewById(R.id.jokes_details);

        }
    }

    class PictureViewHolder extends RecyclerView.ViewHolder {
        TextView name, like, dislike, comment, forward, content, type;
        NetworkImageView imageView;
        LinearLayout personHome;
        RelativeLayout detailHome;
        ImageView imageViewP;

        public PictureViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.picture_person_name);
            like = (TextView) itemView.findViewById(R.id.picture_like_num);
            dislike = (TextView) itemView.findViewById(R.id.picture_dislike_num);
            comment = (TextView) itemView.findViewById(R.id.picture_comments_num);
            forward = (TextView) itemView.findViewById(R.id.picture_forwarding_num);
            content = (TextView) itemView.findViewById(R.id.picture_content);
            type = (TextView) itemView.findViewById(R.id.picture_content_type);
            imageView = (NetworkImageView) itemView.findViewById(R.id.picture_person_picture);
            personHome = (LinearLayout) itemView.findViewById(R.id.picture_person_home);
            detailHome = (RelativeLayout) itemView.findViewById(R.id.picture_details);
            imageViewP = (ImageView) itemView.findViewById(R.id.picture_content_picture);
        }

    }

    class VideoViewHolder extends RecyclerView.ViewHolder {
        TextView name, like, dislike, comment, forward, content, type;
        NetworkImageView imageView, videoImageView;
        VideoView videoView;
        LinearLayout personHome;
        RelativeLayout detailHome;
        ImageView imageViewPlay;
        /**
         * 加载预览进度条
         */
        ProgressBar progressBar;
        public VideoViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.video_person_name);
            like = (TextView) itemView.findViewById(R.id.video_like_num);
            dislike = (TextView) itemView.findViewById(R.id.video_dislike_num);
            comment = (TextView) itemView.findViewById(R.id.video_comments_num);
            forward = (TextView) itemView.findViewById(R.id.video_forwarding_num);
            content = (TextView) itemView.findViewById(R.id.video_content);
            type = (TextView) itemView.findViewById(R.id.video_content_type);
            imageView = (NetworkImageView) itemView.findViewById(R.id.video_person_picture);
            videoView = (VideoView) itemView.findViewById(R.id.video_jokes_content);
            videoImageView = (NetworkImageView) itemView.findViewById(R.id.video_image_picture);
            personHome = (LinearLayout) itemView.findViewById(R.id.video_person_home);
            detailHome = (RelativeLayout) itemView.findViewById(R.id.video_details);
            //初始化进度条
            progressBar= (ProgressBar) itemView.findViewById(R.id.progressBar);
            progressBar.bringToFront();
            imageViewPlay = (ImageView) itemView.findViewById(R.id.play_video);
            imageViewPlay.bringToFront();

        }
    }

    class PictureViewHolderNull extends RecyclerView.ViewHolder {
        TextView textView;

        public PictureViewHolderNull(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.null_tv);
        }
    }
}
